package com.pb.poem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.pb.poem.dao.PoemDao;
import com.pb.poem.dao.impl.PoemDaoImpl;
import com.pb.poem.entity.Poem;
import com.pb.poem.plantform.qq.api.QQAPI;
import com.pb.poem.service.impl.PoemServiceImpl;
import com.pb.poem.util.Page;

public class FriendsPoem extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public FriendsPoem() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter pw = response.getWriter();
		
		PoemServiceImpl poemService = new PoemServiceImpl();
		PoemDao poemDao = new PoemDaoImpl();
		poemService.setPoemDao(poemDao);
		
		//获取查询起始位置诗的id
		int id = Integer.parseInt(request.getParameter("pId"));
		//获取用户好友列表
		QQAPI api=new QQAPI();
		api.setOpenid((String) request.getSession().getAttribute("uid"));
		api.setOpenkey((String) request.getSession().getAttribute("sessionkey"));
		List<String> friendIds=api.getFriendIds();
		System.out.println("获取好友集合大小："+friendIds.size());//////////////////////////////
		List<Poem> list = poemService.getFriendPoems(id, friendIds);
		//在诗每句的结尾加上<br />
		for (Poem poem : list) {
			String p = poem.getContent();
			p = p.replaceAll("，", "，<br />");
			p = p.replaceAll("。", "。<br />");
			poem.setContent(p);
		}
		JSONArray ja = new JSONArray(list);
		String json = ja.toString();
		pw.print(json);
		pw.flush();
		pw.close();
	}

}
